<?php

class Ebx_Component_Container {

	protected $_componentName = '';

	protected $_component = null;

	protected $_template = false;

	protected $_params = array();

	protected $_noRender = false;

	protected $_invokedResult = null;

	function __construct($componentName, $templateName = null, $params = null) {
		$this->_componentName = $componentName;
		if ($templateName !== null) {
			$this->setTemplate($templateName);
		}

		if ($params !== null) {
			$this->setParams($params);
		}
	}

	protected function _init() {
		//lazy
		if ($this->_component === null) {
			if (!$this->getTemplate()) {
				$this->setTemplate('.default');
			}

			$this->_component = new CBitrixComponent();
			if (!$this->_component->InitComponent($this->_componentName)) {
				trigger_error("Not init CBitrixComponent", E_USER_ERROR);
			}
		}
	}

	function setTemplate($templateName) {
		$this->_template = $templateName;
	}

	function getTemplate() {
		return $this->_template;
	}

	function setParams($arParams) {
		$this->_params = $arParams;
	}

	function getParams() {
		return $this->_params;
	}

	function setNoRender($flag = true) {
		$this->_noRender = (bool)$flag;
	}

	function isRender() {
		return !$this->_noRender;
	}

	function show() {
		$this->_init();
		return $this->_component->IncludeComponent($this->getTemplate(), $this->getParams());
	}

	function invokeResult($result) {
		$this->_invokedResult = $result;
	}

	function getInvokedResult() {
		return $this->_invokedResult;
	}

	function invoke($template = null, $params = null) {
		$this->_init();

		if (!$template) {
			$template = $this->getTemplate();
		}

		if ($params === null) {
			$params = $this->getParams();
		}

		$params['CONTAINER'] = $this;
		$params['NO_RENDER'] = !$this->isRender();

		$this->_component->IncludeComponent($template, $params);

		return $this->getInvokedResult();
	}

}

?>